<?php
/* ******************************
 *	Overhead In aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');
include('../../../includes/classes/logger.class.php');
$action 	= $_REQUEST['act'];
$error 		= '';
$data 		= '';
$user_id	= $_COOKIE['USERID'];

$_log   	= Logger::instance('../../../log/documents/overhead/in/', Logger::OFF);

switch ($action) {
    case 'get_edit_page':
	    $goods_id	= $_REQUEST['id'];
	    
	    $page		= GetPage(GetGoods($goods_id),$goods_id);
	    
        $data		= array('page'	=> $page);
        
        break;
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    $local_id = $_REQUEST['local_id'];
	    
	    mysql_query("SET @i = 0;");    	    
	    $rResult = mysql_query("SELECT DISTINCT `OD`.`id` AS `id`,
							  			@i := @i + 1 AS `iterator`,
										`OD`.`name` AS `goods_name`,
										`PR`.`name`,
										`OI`.`quantity`,
										`OI`.`price`,
										`OD`.`amount`
								FROM `overhead_detail` AS `OD` LEFT JOIN `production_identity` AS `PI`
									ON `OD`.`name` = `PI`.`rs_name` LEFT JOIN `production` AS `PR`
									ON `PI`.`production_id` = `PR`.`id` LEFT JOIN `overhead_identity` AS `OI`
									ON `OD`.`overhead_id` = `OI`.`overhead_id` && `PI`.`id` = `OI`.`identity_id` && `OD`.`id` = `OI`.`overhead_detail_id`
								WHERE `OD`.`overhead_id` = $local_id");
		
		$data = array(
			"aaData"	=> array()
		);
		
		if(!$rResult){
		    $error = 'Invalid query: ' . mysql_error();
		}else{
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}
		}
		
        break;
    case 'identity':
    	$id 			= $_REQUEST['ident_id'];
    	$overhead_id 	= $_REQUEST['id'];
    	$overhead_det_id 	= $_REQUEST['det_id'];
		$goods_name		= htmlspecialchars($_REQUEST['gn'], ENT_QUOTES);
		$prod_name		= htmlspecialchars($_REQUEST['gln'], ENT_QUOTES);
		$prod_quantity	= $_REQUEST['glq'];
		$prod_price		= $_REQUEST['glup'];

		$prod_id		= GetProductID($prod_name);		
		ProdIdentity($user_id, $goods_name, $prod_id);		
		$identity_id	= GetIdentityID($goods_name);
		
// 		if(empty($id)){
// 			OverheadIdentityIn($user_id, $overhead_id, $identity_id, $prod_quantity, $prod_price);
// 			$_log->logError('ERR: 1');
// 		}else {
// 			OverheadIdentityUp($user_id, $id, $overhead_id, $identity_id, $prod_quantity, $prod_price);
// 			$_log->logError('ERR: 2');
// 		}

		if(!CheckOverheadIdentity($overhead_det_id)){
			OverheadIdentityIn($user_id, $overhead_id, $overhead_det_id, $identity_id, $prod_quantity, $prod_price);
			//$_log->logError('ERR: 1');
		}else {
			OverheadIdentityUp($user_id, $id, $overhead_id, $overhead_det_id, $identity_id, $prod_quantity, $prod_price);
			//$_log->logError('ERR: 2');
		}
		
		
        break;
        
	case 'get_unit':
		$name = $_REQUEST['name'];
		$unit = GetUnitProd($name);		
		$data 		= array('unit'	=> $unit);
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Overhead In Functions
 * ******************************
 */

function GetUnitProd($name)
{
	$res = mysql_fetch_assoc(mysql_query("	SELECT 		production_unit.`name`
											FROM 		production
											LEFT JOIN 	production_unit ON production.unit = production_unit.id
											WHERE 		production.`name` = '$name'"));
	return $res['name'];
}

function ProdIdentity($user_id, $goods_name, $prod_id)
{
	mysql_query("UPDATE `production_identity`
	             SET    `user_id`		= $user_id,
	             		`production_id` = $prod_id
	             WHERE  `rs_name`		= '$goods_name'");
}

function OverheadIdentityIn($user_id, $overhead_id, $overhead_det_id, $identity_id, $prod_quantity, $prod_price)
{
	mysql_query("INSERT INTO `overhead_identity`
					(`user_id`, `overhead_id`, `overhead_detail_id`, `identity_id`, `quantity`, `price`) 
				 VALUES
					($user_id, $overhead_id, $overhead_det_id, $identity_id, $prod_quantity, $prod_price)");
	//($user_id, $overhead_id, $overhead_det_id, $identity_id, $prod_quantity, $prod_price)");
}

function OverheadIdentityUp($user_id, $id, $overhead_id, $overhead_det_id, $identity_id, $prod_quantity, $prod_price)
{
	mysql_query("UPDATE `overhead_identity`
	             SET    `user_id`		= $user_id,
	             		`overhead_id`	= $overhead_id,
	             		`identity_id`	= $identity_id,
	             		`quantity`		= $prod_quantity,
	             		`price`			= $prod_price
	             WHERE  `id`			= $id");
	
}

function CheckOverheadIdentity($overhead_det_id){
	$check = false;
	$result = mysql_query("SELECT 	overhead_identity.id
				 FROM    	overhead_identity
				 WHERE   	overhead_identity.overhead_detail_id = $overhead_det_id");
	if (mysql_num_rows($result) > 0) {
		$check = true;
	}
	return $check;
}

function GetUnit($point)
{
	$data = '';
    $req = mysql_query("SELECT `id`, `name`
							FROM `production_unit`");
										
	if($point == ''){
		$data = '<option value="0" selected="selected"></option>';
	}
		
	while( $res = mysql_fetch_assoc($req)){			
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}				
	}
		
	return $data;
}

function GetVat($point)
{
	$data = '';
    $req = mysql_query("SELECT `id`, `name`
							FROM `overhead_vat`");
		
	while( $res = mysql_fetch_assoc($req)){			
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}				
	}
		
	return $data;
}

function GetProductID($prod_name)
{ 
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`
										  FROM `production`
										  WHERE `name` = '$prod_name'"));
	return $res['id'];
}

function GetIdentityID($goods_name)
{ 
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`
										  FROM `production_identity`
										  WHERE `rs_name` = '$goods_name'"));
	return $res['id'];
}

function GetGoods($goods_id) 
{
    $res = mysql_query("	SELECT  	`OD`.`id`,
    									`OI`.`id` AS `identity_id`,
										`OD`.`bar_code`,
										`OD`.`name`,
										`PR`.`name` AS `prod_name`,
										`OD`.`unit_id`,
										`PU`.`name` AS `unit`,
										`OD`.`quantity`,
										`OD`.`price`,
										`OD`.`amount`,
										`OD`.`vat_type`,
										`OI`.`quantity` AS `i_quantity`,
										`OI`.`price` AS `i_price`,
    									`OI`.`overhead_detail_id` AS `overhead_detail_id`
							FROM		`overhead_detail` AS `OD` 
							LEFT JOIN 	`production_identity` AS `PI`ON `OD`.`name` = `PI`.`rs_name` 
							LEFT JOIN 	`production` AS `PR` ON `PI`.`production_id` = `PR`.`id` 
							LEFT JOIN 	`production_unit` AS PU ON PR.unit =  PU.id
							LEFT JOIN 	`overhead_identity` AS `OI` ON `OD`.`id` = `OI`.`overhead_detail_id` && `PI`.`id` = `OI`.`identity_id`
							WHERE 		`OD`.`id` = $goods_id");
	return mysql_fetch_assoc($res);
}

function GetPage($res, $goods_id)
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
	    	
	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="goods_code">საქონლის კოდი</label></td>
					<td>
						<input type="text" id="goods_code" class="idle" disabled="disabled" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['bar_code'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_name">დასახელება ზედნადებით</label></td>
					<td>
						<div class="seoy-row" id="goods_name_seoy">
							<input type="text" id="goods_name" class="idle seoy-address" disabled="disabled" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
							<button id="goods_name_btn" class="combobox" disabled="disabled">goods_name</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_measure">ზომის ერთეული ზენადებით</label></td>
					<td>
						<select id="goods_measure" class="idls num" disabled="disabled">' . GetUnit($res[unit_id]) . '</select>
					</td>
				</tr>					
				<tr>
					<td style="width: 170px;"><label for="goods_local_name">საქონლის დასახელება</label></td>
					<td>
						<div class="seoy-row" id="goods_local_name_seoy">
							<input type="text" id="goods_local_name" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['prod_name'] . '"/>
							<button id="goods_local_name_btn" class="combobox">goods_local_name</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_local_unit">ზომის ერთეული</label></td>
					<td>
						<input type="text" id="goods_local_unit" class="idle num" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['unit'] . '" disabled="disabled"/>
					</td>
				</tr>
				';
	
	if(empty($res['i_quantity']) && empty($res['i_price'])){
		$data .= '
				<tr>
					<td style="width: 170px;"><label for="goods_local_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="goods_local_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['quantity'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_local_unit_price">ერთეულის ფასი</label></td>
					<td>
						<input type="text" id="goods_local_unit_price" class="idle num" disabled="disabled" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['price'] . '" />
					</td>
				</tr>';
	}else{
		$data .= '
				<tr>
					<td style="width: 170px;"><label for="goods_local_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="goods_local_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['i_quantity'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_local_unit_price">ერთეულის ფასი</label></td>
					<td>
						<input type="text" id="goods_local_unit_price" class="idle num" disabled="disabled" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['i_price'] . '" />
					</td>
				</tr>';
	}
		
	$data .= '
				<tr>
					<td style="width: 170px;"><label for="goods_price">საქონლის ფასი</label></td>
					<td>
						<input type="text" id="goods_price" class="idle num" disabled="disabled" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['amount'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_vat">დაბეგვრა</label></td>
					<td>
						<select id="goods_vat" class="idls medium" disabled="disabled">' . GetVat($res[vat_type]) . '</select>
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="identity_id" value="' . $res['identity_id'] . '" />
			<input type="hidden" id="overhead_detail_id" value="' . $goods_id . '" />
			<input type="hidden" id="overhead_detail_id_local" value="' . $res['overhead_detail_id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>